/*******************************************************************************
 * Copyright (c) 2012 Clever Devices.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 * 	Clever Devices Interns - 
 * 	Alex Frankel, Andria Young, Kevin Thomas, Nicholas Culpepper, Taner Oztimurlenk, Thomas Volberg
 ******************************************************************************/
package com.framework.cleverstop_tentative;

import java.util.ArrayList;

import android.app.AlarmManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.framework.fares.FarePaymentActivity;
import com.framework.menu.MenuAdapter;
import com.framework.menu.MenuItem;
import com.slidingmenu.lib.SlidingMenu;
import com.slidingmenu.lib.app.SlidingActivity;
//import android.R;
//import com.slidingmenu.example.R;

/**
 * Ended up being replaced by BusTimeMainActivity.  This only loaded a CD watermark image
 * as its main content view.  The team decided it made more sense to be on the bustime page.
 * 
 * @author AFrankel
 *
 */
public class MainMenu extends SlidingActivity implements OnItemClickListener {

	NotificationManager nmMain;
	static final int uniqueMainID = 5000;
	
	public MainMenu(){
		super();
	}
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// sets main view
		setContentView(R.layout.main_watermark);
	    nmMain = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		setSlidingMenu();
		
		Drawable d = this.getResources().getDrawable(R.drawable.layout_background);
		getActionBar().setBackgroundDrawable(d);
		getActionBar().setHomeButtonEnabled(true);	
	}

	/**
	 * INCOMPLETE. Starts an alert that notifies the user the bus destination is approaching
	 * @param view - the view the alert will show up in
	 */
	 public void startAlert(View view) {
		  Intent intent = new Intent(this, MyBroadcastReceiver.class);
		  PendingIntent pendingIntent = PendingIntent.getBroadcast(
		    this.getApplicationContext(), 234324243, intent, 0);
		  LayoutInflater inflater = getLayoutInflater();
			View layout = inflater.inflate(R.layout.customer_lost_notification, (ViewGroup) findViewById(R.id.toast_layout_root));
			ImageView image = (ImageView) layout.findViewById(R.id.ivLostNotif);
			image.setImageResource(R.drawable.ic_launcher);
			TextView text = (TextView) layout.findViewById(R.id.tvLostNotif);
			text.setText("Alarm set in 5 seconds");
			Context contextLost = getApplicationContext();
			  AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
			  alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
			    + (5000), pendingIntent);
		  Toast toast = new Toast(getApplicationContext());
		  toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
		  toast.setDuration(Toast.LENGTH_LONG);
		  toast.show();
//		  Toast.makeText(this, "Alarm set in 5 seconds",
//				    Toast.LENGTH_LONG).show();
		 }


	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		toggle();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int index, long id) {
		// TODO Auto-generated method stub
		switch (index) {

		// this is where each intent will be started for all of your
		// respective sections. Look to the "BUSTIME" case as a model
		case REWARDS:
			Intent Rewards = new Intent("com.framework.rewards.REWARDS");
			startActivity(Rewards);
			break;
		case BOARDS:
			Intent Boards = new Intent("com.framework.customerservice.CUSTOMER");
			startActivity(Boards);
			break;
		case FARES:
			Intent Fares = new Intent("com.framework.cleverstop_tentative.FARESMAINACTIVITY");
			startActivity(Fares);
			break;
		case BUSTIME:
			Intent BusTime = new Intent(
					"com.framework.cleverstop_tentative.BUSTIMEMAINACTIVITY");
			startActivity(BusTime);
			break;
		case 5:
			Intent nfc = new Intent(MainMenu.this,FarePaymentActivity.class);
			startActivity(nfc);
			break;
		}
	}

	ListView mainList;
	MenuAdapter listData;
	ListAdapter realListData;
	Button test;

	public final int REWARDS = 0;
	public final int BOARDS = 1;
	public final int FARES = 2;
	public final int BUSTIME = 3;

	/**
	 * Sets up properties for the sliding menu this will keep properties
	 * consistent across all activities
	 */
	public void setSlidingMenu() {
		// sets view that slides out from left, this will be standard across all
		// menus
		// allowing for quick and intuitive navigation
		setBehindContentView(R.layout.persistent_menu_list);
		
		
		MenuItem[] menuItems = {
			new MenuItem("Rewards",R.drawable.rewaredsicon, "1"),
			new MenuItem("Boards",R.drawable.boardsicon, ""),
			new MenuItem("Fares", R.drawable.faresicon, ""),
			new MenuItem("BusTime", R.drawable.bustimeicon, ""),
			new MenuItem("Settings", R.drawable.settinsicon, ""),
			new MenuItem("NFC ACTIVITY", R.drawable.settinsicon, "")
		};
				
		ArrayList<MenuItem> items = new ArrayList<MenuItem>();
		for(int i = 0; i<menuItems.length;i++)
			items.add(menuItems[i]);
		
		listData = new MenuAdapter(this,R.layout.listview_item_row,items);
		
		mainList = (ListView) findViewById(R.id.menuList);

		// dynamically add items to listView
		// did below as a test, but can easily add items based on the section of
		// app you are in
		// listData.insert("BusTime", list.size());

		mainList.setAdapter(listData);
		mainList.setOnItemClickListener(this);

		getSlidingMenu().setBehindOffsetRes(R.dimen.DistanceFromSide);
		getSlidingMenu().setBehindScrollScale(0.5f);
		getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		getSlidingMenu().setActivated(true);

	}

	@Override
	public boolean onOptionsItemSelected(android.view.MenuItem item) {
		toggle();
		return true;
	}
	
	
}
